﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using FlickrNet;
using System.Configuration;

namespace Flicker_Application.Web
{
    /// <summary>
    /// Summary description for FlickerService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class FlickerService : System.Web.Services.WebService
    {

        //[WebMethod]
        //public string HelloWorld()
        //{
        //    return "Hello World";
        //}


        [WebMethod]
        public List<Photo> GetPhotosByName(string username)
        {
            Flickr flickr = new Flickr();
            string flickrUserId = string.Empty;
            List<Photo> pics = new List<Photo>();
            FoundUser userInfo;
            try
            {
                //if (username.Contains('@'))
                //{
                //    userInfo = flickr.PeopleFindByEmail(username);
                //}
                //else
                //{
                //    userInfo = flickr.PeopleFindByUserName(username);
                //}
                //flickrUserId = userInfo.UserId;

                // Get 200 most recent pictures
                PhotoSearchOptions options = new PhotoSearchOptions();
                options.Tags = username;
                options.PerPage = 200;
                //get 10 pics randomly
                PhotoCollection searchResults = flickr.PhotosSearch(options);
                pics = (from p in searchResults.OrderBy(p => Guid.NewGuid())
                        select new Photo
                        {
                            PhotoId = p.PhotoId,
                            Title = p.Title,
                            MediumUrl = p.MediumUrl,
                            WebUrl = p.WebUrl,
                            LargeUrl = p.LargeUrl,
                            ThumbnailUrl = p.ThumbnailUrl,
                            SmallUrl = p.SmallUrl
                        }).Take(int.Parse(ConfigurationManager.AppSettings["Result_Count"])).ToList();

            }
            catch (FlickrApiException e)
            {
                // Username not found.
                // send error message to caller
                return null;
            }

            return pics;
        }


        [WebMethod]
        public List<Comments> GetComments(string pid)
        {
            Flickr flickr = new Flickr();
            string flickrUserId = string.Empty;
            List<Comments> comments = new List<Comments>();
            try
            {
                PhotoCommentCollection commentResults = flickr.PhotosCommentsGetList(pid);
                comments = (from c in commentResults
                            select new Comments
                            {
                                CommentHtml = c.CommentHtml,
                                DateCreated = c.DateCreated,
                                PhotoId = pid,
                                AuthorUserName = c.AuthorUserName,
                                CommentId = c.CommentId
                            }).ToList();

                
            }
            catch (FlickrApiException e)
            {
                // error
                return null;
            }

            return comments;
        }
    }
}
